package com.whc.address.controller;

import com.whc.address.domain.dto.AddressDto;
import com.whc.address.domain.entity.Address;
import com.whc.address.service.impl.AddressServiceImpl;
import com.whc.common.context.BaseContext;
import com.whc.common.domain.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/client/address")
@Api("地址接口管理")
public class AddressController {

    @Autowired
    private AddressServiceImpl addressService;

    @ApiOperation("查询用户的地址薄")
    @GetMapping("/getAll")
    public Result<List<Address>> getAll(){
        Long currentId = BaseContext.getCurrentId();
        if(currentId==null||currentId==0)return Result.error("用户未登录");
        List<Address> addresses = addressService.selectAddress(currentId);
        return Result.success(addresses);
    }

    @ApiOperation("添加用户地址")
    @PostMapping("/add")
    public Result addAddress(@RequestBody AddressDto addressDto){
        addressService.addAddress(addressDto);
        return Result.success();
    }

    @ApiOperation("修改用户地址")
    @PostMapping("/update")
    public Result updateAddress(@RequestBody AddressDto addressDto){
        addressService.updateAddress(addressDto);
        return Result.success();
    }

    @ApiOperation("删除地址")
    @PostMapping("/delete/{id}")
    public Result deleteAddress(@PathVariable("id")Long id){
        addressService.deleteAddress(id);
        return Result.success();
    }

    @ApiOperation("根据id查询地址")
    @GetMapping("select/{id}")
    public Result<Address> selectAddressById(@PathVariable("id")Long id){
        Address address=addressService.selectAddressById(id);
        return Result.success(address);
    }
}
